package client.main;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;

import client.calendar.CalendarPane;
import client.login.Login;
import client.meetingview.AppointmentPane;
import client.messages.MessagePane;

import javax.swing.JButton;

import miscellaneous.Storage;

/**
 * Main window of the application.
 * @author Erik
 *
 */
public class MainWindow extends JFrame {

	private JPanel contentPane;
	private CalendarPane calendarPane;
	private JTabbedPane tabbedPane;
	
	/**
	 * Create the frame.
	 */
	public MainWindow() {
		if(Storage.getloggedInUser() == null) {
			JOptionPane.showMessageDialog(this, "Ingen bruker logget inn. Avslutter");
			System.exit(0);
		};
		setTitle("Awesome Calendar (aCalendar)	|	Logget inn: " + Storage.getloggedInUser());
		setIconImage(Toolkit.getDefaultToolkit().getImage("resources/icons/aCalendarIcon.png"));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 1010, 598);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		addCalendar();
		addMessages();
		addAppointments();
		addSettings();
		
		contentPane.add(tabbedPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		JButton btnNewButton = new JButton("Logg ut");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				logout();	
			}
		});
		panel.add(btnNewButton);
		
		
		addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				
				logout();
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void logout() {
		int a = JOptionPane.showConfirmDialog(null, "Er du sikker på at du vil logge ut og slå av programmet?", "Bekreftelse", JOptionPane.YES_NO_OPTION);
		if(a == JOptionPane.YES_OPTION) {
			Login.logout();
		}
		return;
	}
	
	private void addCalendar() {
		calendarPane = new CalendarPane(Storage.getloggedInUser());
		tabbedPane.add(calendarPane);
		tabbedPane.setTitleAt(0, "Kalendar");
		
	}
	
	private void addMessages() {
		JPanel messages = new MessagePane();
		tabbedPane.add(messages);
		tabbedPane.setTitleAt(1, "Meldinger");
	}
	
	private void addAppointments() {
		AppointmentPane appointments = new AppointmentPane();
		tabbedPane.add(appointments);
		tabbedPane.setTitleAt(2, "Avtaleliste");
	}
	
	private void addSettings() {
		JPanel settings = new JPanel();
		tabbedPane.add(settings);
		tabbedPane.setTitleAt(3, "Innstillinger");
	}
}
